package com.youlemall.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.youlemall.common.pojo.PageResult;
import com.youlemall.pojo.Specification;
import com.youlemall.pojo.SpecificationOption;
import com.youlemall.service.SpecificationOptionService;
import com.youlemall.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 2 * @Author: kevin
 * 3 * @Date: 2019/7/14 22:17
 * 4
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

	@Reference
	private SpecificationService specificationService;

	@Reference
	private SpecificationOptionService specificationOptionService;

	/**
	 * 分页查询 规格类
	 * @param specification 规格类
	 * @param page 当前页
	 * @param row 页大小
	 * @return 分页工具类
	 */
	@GetMapping("/findByPage")
	public PageResult findByPage(Specification specification, Integer page,
								 @RequestParam(defaultValue = "10") Integer row) {
		return specificationService.findByPage(specification, page, row);
	}


	/**
	 * 规格保存
	 * @param specification
	 * @return
	 */
	@PostMapping("/save")
	public Boolean saveSpecification(@RequestBody Specification specification){
		try {
			specificationService.save(specification);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 规格修改
	 * @param specification
	 * @return
	 */
	@PostMapping("/update")
	public Boolean updateSpecification(@RequestBody Specification specification){
		try {
			specificationService.update(specification);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 回显规格选项
	 * @param id
	 * @return
	 */
	@GetMapping("/findOptions")
	public List<SpecificationOption> findSpeOptions(Long id){
		return specificationOptionService.findSpeOptions(id);
	}

	/**
	 *删除规格类
	 * @param ids
	 * @return
	 */
	@GetMapping("/delete")
	public Boolean delSpecification(Long[] ids){
		try {
			specificationService.deleteAll(ids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@GetMapping("/findSpeList")
	public List<Map<String,Object>> findSpeList(){
		return specificationService.findSpeNameAndId();
	}

}
